package com.yq.cp.controller.basics;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yq.cp.config.UserThreadLocal;
import com.yq.cp.entity.basics.SecureTrainingRecords;
import com.yq.cp.entity.basics.model.SecureListExcel;
import com.yq.cp.entity.common.ApiResult;
import com.yq.cp.entity.common.PageVo;
import com.yq.cp.entity.permission.User;
import com.yq.cp.service.basics.SecureTrainingRecordsService;
import com.yq.cp.utils.BeanCopyUtil;
import com.yq.cp.utils.ExcelUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 安全培训记录管理 前端控制器
 * </p>
 *
 * @author shenyuqing
 * @since 2024-09-03
 */
@RestController
@RequestMapping("/secureTrainingRecords")
public class SecureTrainingRecordsController {
    @Resource
    SecureTrainingRecordsService secureTrainingRecordsService;

    /**
     * 添加编辑
     * @param secureTrainingRecords
     * @return
     */
    @PostMapping("add")
    public ApiResult add(@RequestBody SecureTrainingRecords secureTrainingRecords){
        if (Objects.isNull(secureTrainingRecords.getId()))
            secureTrainingRecords.setCreateUserId(UserThreadLocal.get().getId());
        secureTrainingRecordsService.saveOrUpdate(secureTrainingRecords);
        return ApiResult.success();
    }

    /**
     * 列表
     * @param vo
     * @return
     */
    @PostMapping("list")
    public ApiResult list(@RequestBody PageVo vo){
        User user = UserThreadLocal.get();
        Page<SecureTrainingRecords> page = new Page<>(vo.getPage(), vo.getSize());
        QueryWrapper<SecureTrainingRecords> wrapper = new QueryWrapper<>();
        if (!Objects.isNull(vo.getLike())&&!"".equals(vo.getLike()))
            wrapper.like("title",vo.getLike());
        wrapper.eq("site_id",vo.getSiteId());
        if (!Objects.isNull(vo.getType()))
            wrapper.eq("create_user_id",user.getId());
        wrapper.orderByDesc("create_time");
        page = secureTrainingRecordsService.page(page,wrapper);
        if (!page.getRecords().isEmpty()){
            for (SecureTrainingRecords record : page.getRecords()) {
                if (record.getCreateUserId().equals(user.getId()))
                    record.setIsSelf(1);
                else
                    record.setIsSelf(0);
            }
        }
        return ApiResult.success(page);
    }

    /**
     * 删除
     * @param secureManagement
     * @return
     */
    @PostMapping("remove")
    public ApiResult remove(@RequestBody SecureTrainingRecords secureManagement){
        secureTrainingRecordsService.removeById(secureManagement);
        return ApiResult.success();
    }

}

